// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.linqingying.gitcode.issue

import com.intellij.openapi.progress.ProgressIndicator
import com.linqingying.gitcode.api.GCRepositoryPath
import com.linqingying.gitcode.api.GitCodeApiRequestExecutor
import com.linqingying.gitcode.api.GitCodeApiRequests
import com.linqingying.gitcode.api.GitCodeServerPath
import com.linqingying.gitcode.api.data.GitCodeIssue
import com.linqingying.gitcode.api.data.GitCodeSearchedIssue
import com.linqingying.gitcode.api.util.GitCodeApiPagesLoader
import java.io.IOException

object GitCodeIssuesLoadingHelper {
  @JvmOverloads
  @JvmStatic
  @Throws(IOException::class)
  fun load(executor: GitCodeApiRequestExecutor, indicator: ProgressIndicator, server: GitCodeServerPath,
           owner: String, repo: String, withClosed: Boolean, maximum: Int = 100, assignee: String? = null): List<GitCodeIssue> {
    return GitCodeApiPagesLoader.load(executor, indicator,
                                     GitCodeApiRequests.Repos.Issues.pages(server, owner, repo,
                                                                           if (withClosed) "all" else "open", assignee), maximum)
  }

  @JvmOverloads
  @JvmStatic
  @Throws(IOException::class)
  fun search(executor: GitCodeApiRequestExecutor, indicator: ProgressIndicator, server: GitCodeServerPath,
             owner: String, repo: String, withClosed: Boolean, assignee: String? = null, query: String? = null)
    : List<GitCodeSearchedIssue> {

    return GitCodeApiPagesLoader.loadAll(executor, indicator,
                                        GitCodeApiRequests.Search.Issues.pages(server,
                                                                              GCRepositoryPath(owner,
                                                                                               repo),
                                                                              if (withClosed) null else "open", assignee, query))
  }
}
